<template>
  <div class="app-container">
    <!-- 添加 -->
    <el-form ref="form" label-width="120px">
      <el-form-item label="医院名称">
        <el-input v-model="hospitalSet.hosname" />
      </el-form-item>

      <el-form-item label="医院编号">
        <el-input v-model="hospitalSet.hoscode" />
      </el-form-item>

      <el-form-item label="api基础路径">
        <el-input v-model="hospitalSet.apiUrl" />
      </el-form-item>

      <el-form-item label="联系人姓名">
        <el-input v-model="hospitalSet.contactsName" />
      </el-form-item>
      <el-form-item label="联系人手机">
        <el-input v-model="hospitalSet.contactsPhone" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="updataOrSave()">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import hospSet from "@/api/hospset";

export default {
  data() {
    return {
      hospitalSet: {}, // 修改的数据
    };
  },
  methods: {

    // 判断是添加还是修改
    updataOrSave(){
      if(this.hospitalSet.id){
        this.updateById()
      }else {
        this.save()
      }
    },

    // 添加
    save(){
      hospSet.saveById(this.hospitalSet).then(response => {
        this.$message({
          type: 'success',
          message: '添加成功'
        })

        this.$router.push("/hospSet/list")
      })
    },

    // 更新
    updateById() {
      hospSet
        .updateById(this.hospitalSet)
        .then((response) => {
          this.$message({
            // ?
            type: "success",
            message: "修改成功",
          });

          //跳转列表页面，使用路由跳转方式实现
          this.$router.push({ path: "/hospSet/list" });
        })
        .catch((error) => {
          alert(error);
        });
    },

    // 查询
    getById() {
      // ?
      if (this.$route.params.id) {
        var id = this.$route.params.id;

        hospSet.getById(id).then((response) => {
          this.hospitalSet = response.data;
        });
      }else {
        this.hospitalSet = {}
      }

      console.log("id", id);
    },

    //添加
    hospSetAdd() {
      hospset
        .hospSetAdd(this.hospitalSet)
        .then((response) => {
          this.$message({
            type: "success",
            message: "添加成功",
          });
        })
        .catch((error) => {
          alert(error);
        });
    },
  },
  //在页面渲染之前执行
  created() {
    this.getById();
  },
};
</script>
